package org.smart4j.framework.util;

import com.sun.net.httpserver.spi.HttpServerProvider;
import org.smart4j.framework.bean.FormParam;
import org.smart4j.framework.bean.Param;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

/**
 * 请求助手类
 *
 * Created by chinalin on 2017/3/27.
 */
public class RequestHelper {
    /**
     * 创建请求对象
     */
    public static Param createParam(HttpServletRequest request) throws IOException{
        List<FormParam> formParamList = new ArrayList<FormParam>() ;
        formParamList.addAll(parseParameterNames(request));
        formParamList.addAll(parseInputStream(request));
        return new Param(formParamList) ;
    }

    private static List<FormParam> parseParameterNames(HttpServletRequest request){
        List<FormParam> formParamList = new ArrayList<FormParam>() ;
        //创建请求参数对象
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()){
            String fieldName = paramNames.nextElement();
            String[] fieldValues = request.getParameterValues(fieldName) ;
            if (ArrayUtil.isNotEmpty(fieldValues)){
                Object fieldValue ;
                if (fieldValues.length == 1){
                    fieldValue = fieldValues[0];
                }else {
                    StringBuilder sb = new StringBuilder("") ;
                    for (int i = 0 ; i < fieldValues.length; i++){
                        sb.append(fieldValues[i]) ;
                        if (i != fieldValues.length - 1){
                            sb.append(StringUtil.SEPARATOR) ;
                        }
                    }
                    fieldValue = sb.toString() ;
                }
                formParamList.add(new FormParam(fieldName,fieldValue)) ;
            }
        }
        return formParamList ;
    }
    /**
     * 测试req.getParameterNames()和req.getInputStream()
     * 当浏览器get提交时 req.getParameterNames()可以读到，而req.getInputStream()读不到参数
     * 当post提交时 req.getParameterNames()可以读到参数，读完后，req.getInputStream()读不到了，如果req.getParameterNames()没执行，那么req.getInputStream()就可以读到参数了
     */
    private static List<FormParam> parseInputStream(HttpServletRequest request) throws IOException{
        List<FormParam> formParamList = new ArrayList<FormParam>() ;
        String body = CodecUtil.decodeURL(StreamUtil.getString(request.getInputStream())) ;
        if (StringUtil.isNotEmpty(body)){
            String[] kvs = StringUtil.splitString(body, "&") ;
            if (ArrayUtil.isNotEmpty(kvs)){
                for (String kv : kvs){
                    String[] array = StringUtil.splitString(kv, "=") ;
                    if (ArrayUtil.isNotEmpty(array) && array.length == 2){
                        String fieldName = array[0] ;
                        String fieldValue = array[1] ;
                        formParamList.add(new FormParam(fieldName,fieldValue)) ;
                    }
                }
            }
        }
        return formParamList ;
    }
}
